package com.itheima.tliasbackend.service.impl;

import com.itheima.tliasbackend.mapper.DeptMapper;
import com.itheima.tliasbackend.mapper.EmpMapper;
import com.itheima.tliasbackend.pojo.Dept;
import com.itheima.tliasbackend.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

// 部门业务实现类
@Slf4j
@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private EmpMapper empMapper;

    @Override
    public List<Dept> getList() {
        Dept dept = deptMapper.selectList();
        return new ArrayList<>();
    }

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW) // 默认捕获的是运行时错误
    @Override
    public void removeById(Integer id) {
        // 通过ID删除部门
        deptMapper.deleteById(id);

        // 删除部门员工
        empMapper.deleteByDeptId(id);
    }

    @Override
    public void addOne(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        dept.setCreateTime(LocalDateTime.now());
        deptMapper.insert(dept);
    }
}